package com.idearworld.controller;

import static com.idearworld.utils.ControllerUtil.failedResult;
import static com.idearworld.utils.ControllerUtil.successResult;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.idearworld.beans.GlobalConstants;
import com.idearworld.beans.SplitPage;
import com.idearworld.model.Idears;
import com.idearworld.model.UserInfo;
import com.idearworld.service.IdearService;
import com.idearworld.utils.ControllerUtil;

@Controller
@RequestMapping("/idear")
public class IdearAdminCtrl {

	@Autowired
	private IdearService idearService;

	@RequestMapping(value = "/FollowIdearAction", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> followIdear(HttpSession session) {
		UserInfo user = ControllerUtil.getLoginUser(session);
		// this.idearService.followIdear(Integer.valueOf(idearId), followerId);
		return successResult("Followed Successful!");
	}

	@RequestMapping(value = "/Latest",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> browseIdear() {
		List<Idears> list = this.idearService.browseIdear(new SplitPage(0, 5));
		return successResult(list);
	}

	@RequestMapping(value = "/ListUserIdears/{pageNum}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> listUserIdears(@PathVariable String pageNum,
			HttpSession session) {
		UserInfo user = ControllerUtil.getLoginUser(session);
		if (user == null) {
			return failedResult(GlobalConstants.ERROR_TYPE_NEED_LOGIN,
					"need login!");
		}

		List<Idears> list = this.idearService.listUserIdears(user,
				new SplitPage(pageNum));

		return successResult(list);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	@ResponseStatus(HttpStatus.CREATED)
	@ResponseBody
	public Map<String, Object> createIdear(@RequestBody Idears idear,HttpSession session) {
		try {
			UserInfo user = ControllerUtil.getLoginUser(session);
			idear.setUserId(user.getUserId());
			idear.setCreateDate(new Date());
			this.idearService.createIdear(idear);
		} catch (Exception e) {
			e.printStackTrace();
			return failedResult(GlobalConstants.ERROR_TYPE_SERVER_ERROR,
					"Server error!");
		}
		return successResult(idear);
	}

	@RequestMapping(value = "{idearId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> loadIdear(@PathVariable("idearId") Integer idearId) {
		Idears idear = null;
		try {
			idear = this.idearService.getIdearById(idearId);
		} catch (Exception e) {
			e.printStackTrace();
			return failedResult(GlobalConstants.ERROR_TYPE_SERVER_ERROR,
					"Server error!");
		}

		return successResult(idear);
	}

}
